package com.face.recognition.controller;

import com.arcsoft.face.FaceInfo;
import com.arcsoft.face.LivenessInfo;
import com.face.recognition.domain.*;
import com.face.recognition.service.FaceRecognitionService;
import com.face.recognition.service.impl.HKNetSdkServiceImpl;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/face")
@CrossOrigin
public class FaceSignInController {

    @Autowired
    private FaceRecognitionService faceRecognitionService;
    @Autowired
    private HKNetSdkServiceImpl hkNetSdkServiceimpl;

    @GetMapping("/sign")
    public String sign(){
        //return  System.getProperty("user.dir") + "\\lib\\HCNetSDK.dll";
        return hkNetSdkServiceimpl.Login() + " " + hkNetSdkServiceimpl.SetupAlarmChan();
    }

    @PostMapping("/register")
    public FaceRecognitionResponse register(MultipartFile file, String id) throws IOException {

        FaceRecognitionResponse faceRecognitionResponse = new FaceRecognitionResponse();

        //判断该id是否已注册人脸
        if(faceRecognitionService.getFace(id) != null) {

            faceRecognitionResponse.setCode(600);
            faceRecognitionResponse.setMsg("注册失败！请查询错误码！");
            return faceRecognitionResponse;
        }

        //MultipartFile转换成File
        String path = "D:\\FaceImg\\Register";
        File registerImage = new File(path,id + "_RegisterImage.jpg");
        FileUtils.copyInputStreamToFile(file.getInputStream(), registerImage);

        return faceRecognitionService.register(registerImage, id);
    }


}
